#ifndef FTPSERVER_H
#define FTPSERVER_H

#include <QObject>
#include <QProcess>
#include <QNetworkSession>

class QNetworkConfigurationManager;

class FtpServer : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool autoOnline READ autoOnline WRITE setAutoOnline)
private:
    explicit FtpServer(QObject *parent = 0);
public:
    virtual ~FtpServer();

    static FtpServer* instance();

    bool autoOnline() const;
    void setAutoOnline(bool enable);

signals:
    void started();
    void stopped();
    void output(const QString &message);

public slots:
    bool start();
    void stop();

    bool hasPemCertificate();

private slots:
    void slotError(QProcess::ProcessError);
    void slotStateChanged(QProcess::ProcessState state);
    void slotReadyReadStandardError();
    void slotReadStandardOutput();
    void slotReadyRead();
    void slotSettingsValueChanged(const QString &name);

    void slotOnlineChanged();
    void slotNetworkUpdated();
    void slotSessionOpened();
    void slotSessionClosed();
    void slotSessionStateChanged(QNetworkSession::State);
    void slotSessionError(QNetworkSession::SessionError error);
    void slotSessionNewActivated();
    void slotSessionPreferredChanged(const QNetworkConfiguration &config, bool isSeamless);

private:
    QString m_pythonExecutable;
    QString m_serverExecutable;
    QProcess *m_process;
    bool m_autoOnline;
    QNetworkConfigurationManager *m_networkConfigManager;
    QNetworkSession *m_networkSession;
    QString m_rootDir;
    bool m_starting;

    QByteArray pemCertificate();

    QString pythonExecutable();
    QString serverExecutable();
};

#endif // FTPSERVER_H
